/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：CommandCopy.js<br>
 * 日期：2018年9月12日<br>
 * 作者: 孙燕宝<br>
 * 功能：显示图片命令<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CommandShowImg<br>
 * 类型功能：显示图片命令<br>
 */
Rainier.UI.V2.CommandShowImg = draw2d.command.Command.extend({
	
	NAME : "Rainier.UI.V2.CommandShowImg",
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandShowImg.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(context,editParts){
		this.context 		= context;
		this.editPart       = editParts.data[0];
		this.rootEditPart 	= this.context.getRootEditPart();
		this.activeEditor = app.getContext().getActiveEditor();
		this.showImgArr = [];
		this.tabId = app.canvasId;
		this._super();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandShowImg.execute<br>
	 * 函数功能：执行命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	execute:function(){
    	var _this = this;
    	var model = this.editPart.getModel();
	//选中主题的位置和属性
	var property = model.property;
	var modelProperty = property.modelProperty;
	var imgArr = modelProperty.imgArr;
	if(imgArr){
		if(imgArr.length>0){
			$.each(imgArr,function(index,id){
				var deleteInfo = _this.getDeleteInfoById(id);
				if(deleteInfo){
					var currentModel 	 = deleteInfo.model;
					var connInfoStack	 = deleteInfo.deleteConnInfoStack.clone();
					if(currentModel instanceof Rainier.UI.V2.DeviceModel){
						var portList 	= deleteInfo.ports.clone();
						portList.each(function(index,port){
							currentModel.addPort(port);
						});

						var parentModel = currentModel.getParent();

						// 撤销时，修改自定义器件的图片
						currentModel.property.imageProperty.svg = currentModel.property.uiProperty.imageString;
						
						parentModel.addChildModel(currentModel);
						var uiProperty = currentModel.getProperty().uiProperty;
						currentModel.setPosition(uiProperty.x,uiProperty.y,false);
					}
					
					//翻转数组，逆序恢复数组中的内容
					connInfoStack.reverse();
					
					//恢复连线：
					connInfoStack.each(function(index,connModelInfo){
						_this.addConnectionModel(connModelInfo);
					})
					_this.rootEditPart.refreshChildren();
					_this.showImgArr.push(deleteInfo);
					var index = hideImgArr[_this.tabId].indexOf(deleteInfo);
					hideImgArr[_this.tabId].splice(index,1);
				}
			});
		}
	}
	// 刷新连线
	if(modelProperty.typeName === "TextModelJson"){
		this.refreshVerticalConnection();
	}
	else if(modelProperty.typeName === "TextModelJsonPara"){
		this.refreshHorizontalConnection();
	}

	//更新预览图
	// if(isSelectedPreview){
	// 	setTimeout(function(){
	// 		var img = $("#img-preview");
	// 		refreshPreview(img);
	// 	});
	// }
	},
    
	/**
	 * 函数名称：Rainier.UI.V2.CommandShowImg.undo<br>
	 * 函数功能：撤销命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	undo:function(){
		var _this = this;
		if(_this.showImgArr.length>0){
			$.each(_this.showImgArr,function(index,deleteInfo){
				var model = deleteInfo.model;
				var parentModel = model.getParent();
				parentModel.removeChildModel(model);
				hideImgArr[_this.tabId].push(deleteInfo);
			});
			this.showImgArr = [];
		}
	},
    
	/**
	 * 函数名称：Rainier.UI.V2.CommandShowImg.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	redo:function(){
		this.execute();
	},

      /**
	 * 函数名称：Rainier.UI.V2.CommandShowImg.getModelById<br>
	 * 函数功能：根据主题id在隐藏图片库查询对应deleteInfo<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	getDeleteInfoById : function(id){
		var _deleteInfo = null;
		var hideArr = hideImgArr[this.tabId];
		if(hideArr){
			if(hideArr.length>0){
				$.each(hideArr,function(index,deleteInfo){
					var _id = deleteInfo.model.m_sUuid;
					if(_id==id){
						_deleteInfo = deleteInfo;
					}
				});
			}
		}
		return _deleteInfo;
	},

	/**
	 * 函数名称：draw2d.command.CommandShowImg.addConnectionModel<br>
	 * 函数功能：恢复连线信息的添加<br>
	 * 参数输入：connModelInfo:封装好的connModelInfo信息<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	addConnectionModel : function(connModelInfo){
		var connType	= connModelInfo.connType;
		var connObj 	= connModelInfo.connModel;
		var sourceModel = connModelInfo.sourceModel;
		var targetModel = connModelInfo.targetModel;
		
		//第一步：把当前连线添加进来
		var conn 	   = connObj.connection;

		//第二步：恢复连线两端的jointModel
		if(sourceModel){
			var jointModel 		 = sourceModel.jointModel;
			var hybridPort		 = sourceModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		if(targetModel){
			var jointModel 		 = targetModel.jointModel;
			var hybridPort		 = targetModel.hybirdPort;
			
			var jointModelId	 = jointModel.getUuid();
			
			var jointModelParent  = jointModel.getParent();
			var connJointModelIds = [];
			var connJointModels   = jointModelParent.getChildren();
			connJointModels.forEach(function(connJointModel,index,connJointModels){
				connJointModelIds.push(connJointModel.getUuid());
			})
			//不存在当前model
			if(connJointModelIds.indexOf(jointModelId) === -1){
				jointModel.addPort(hybridPort);
				jointModelParent.addChildModel(jointModel);    					
			}
		}
		
		//第三步：恢复连线和sourcePort和targetPort的关联关系。
		var sourcePort = connObj.sourcePort;
		var targetPort = connObj.targetPort;
		conn.attachSourcePort(sourcePort);
		conn.attachTargetPort(targetPort);
		sourcePort.attachOutgoing(conn);
		targetPort.attachIncoming(conn);
			
		var connParent = conn.getParent();
		connParent.addConnection(conn);
	},

	/**
	 * 函数名称：Rainier.UI.V2.CommandAddTheme.refreshVerticalConnection<br>
	 * 函数功能：重新绘制连线<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	refreshVerticalConnection : function(){
		//删除错误连线
		//遍历三次，否则不能删除干净
		var connections = this.activeEditor.rootModel.connections.data;
		var _connection = Rainier.UI.V2.Communication.ConnectionModel[0];
		
		for(var j=0;j<3;j++){
			connections = this.activeEditor.rootModel.connections.data;
			for(var i=0;i<connections.length;i++){
				var connection = connections[i];
				// 只刷新竖向连线
				if(connection.property.modelProperty.direction !== "undefined" && connection.property.modelProperty.direction== "vertical"){
					var segments = connection.property.modelProperty.segments;
					if(segments.length>1){
						if(segments[0].type=="vertical"||segments[1].type=="horizontal"){
							//确保新线完成再删除错误
							var complete = false;
							//确保id相同
							_connection.uuid = connection.m_sUuid;
							//删除连线
							var sourcePort = connection.sourcePort;
							var targetPort = connection.targetPort;
							var _segment = connection.property.modelProperty.segments;
							var x = _segment[1].end_x;
							var y = _segment[0].start_y;
							if(sourcePort){
								var segment = _connection.modelProperty.segments;
								segment[1].start_x = x;
								segment[1].start_y = y;
								this.activeEditor.makeConnection(sourcePort,targetPort,_connection);
							}
							complete = true;
							if(complete){
								this.rootEditPart.getModel().removeConnection(connection);
							}
						}
					}
				}
				
			}
		}
	},

	/**
	 * 函数名称：Rainier.UI.V2.CommandAddParaImg.refreshHorizontalConnection<br>
	 * 函数功能：重新绘制连线<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	refreshHorizontalConnection : function(){
		//删除错误连线
		//遍历三次，否则不能删除干净
		var connections = this.activeEditor.rootModel.connections.data;
		var _connection = Rainier.UI.V2.Communication.ConnectionModel[0];
			
		for(var j=0;j<3;j++){
			connections = this.activeEditor.rootModel.connections.data;
			for(var i=0;i<connections.length;i++){
				var connection = connections[i];
				// 只刷新竖向连线
				if(connection.property.modelProperty.direction !== "undefined" && connection.property.modelProperty.direction== "horizontal"){
					var segments = connection.property.modelProperty.segments;
					if(segments.length>1){
						if(segments[0].type=="horizontal"||segments[1].type=="vertical"){
							//确保新线完成再删除错误
							var complete = false;
							//确保id相同
							_connection.uuid = connection.m_sUuid;
							//删除连线
							var sourcePort = connection.sourcePort;
							var targetPort = connection.targetPort;
							var _segment = connection.property.modelProperty.segments;
							var x = _segment[0].start_x;
							var y = _segment[1].end_y;
							if(sourcePort){
								var segment = _connection.modelProperty.segments;
								segment[1].start_x = x;
								segment[1].start_y = y;
								this.activeEditor.makeConnection(sourcePort,targetPort,_connection);
							}
							complete = true;
							if(complete){
								this.rootEditPart.getModel().removeConnection(connection);
							}
						}
					}
				}
			}
		}
	},
});